<template>
	<om-page title="确认App登录">
		<view class="padding-xl text-center text-xxl bg-steelblue">确认App登录</view>
		<view class="content">
			<!-- #ifdef H5 || APP-PLUS -->
			<om-wing-blank>
				<view class="input-group">
					<view class="input-row border" >
						<text class="title om-color">单位全称:</text>
						<m-input v-model="unitName"></m-input>
					</view>
					<!-- <view class="input-row border" >
						<text class="title om-color">账号姓名:</text>
						<m-input v-model="userName"></m-input>
					</view> -->
					<view class="input-row border" >
						<text class="title om-color">私人手机号码:</text>
						<m-input v-model="phone"></m-input>
					</view>
					<view class="input-row border" >
						<text class="title om-color">登录密码:</text>
						<m-input type="password" v-model="password"></m-input>
					</view>
				</view>
				<view style="text-align: right;margin-top: 10px;">
					<view class="cu-tag radius bg-grey light" style="font-size:14px;">
						<navigator url="/pages/login/version-info/version-info">版本记录</navigator>
					</view>
				</view>
				<view style="padding: 10px;"><button type="primary" class="primary" @tap="bindConfirm">确认</button></view>
			</om-wing-blank>
			<!-- #endif -->
		</view>
		<view class="wxts">
			<view>温馨提示：</view>
			<view class="single">1)首次登录OM系统时确认登录；</view>
			<view class="single">2)新员工登录异常时，请联系招聘业务主管查看入职是否审批；</view>
			<view class="single">3)老员工登录异常时，请联系OM系统管理员查看登录授权或修改临时登录密码；</view>
			<view class="single">4)如电脑端可以登录但App登录异常时，请检查网络是否有异常或服务器是否有启动。</view>
		</view>
	</om-page>
</template>

<script>
import md5 from '@/static/js/md5.js';
const form = require("@/static/js/formValidation.js")
import {ignoreHtml, errorModal,confirmModal } from 'static/js/common.js'
import {mapState, mapActions} from 'pinia';
import { useLoginUserStore } from '@/stores/login-user.js'

const app = getApp();
export default {
	data() {
		return {
			unitName:"",
			userName:'',
			phone:"",
			password:'',
			stateurl: '',
			orgSites:[],
			accountFocus: false,
			omMateServer: 'http://139.9.158.175:82/',
			// omMateServer: 'http://192.168.11.154:82/'
		};
	},
	computed: mapState(useLoginUserStore,[
		'forcedLogin',
		'hasLogin', 
		'orgName', 
		'subUrl'
	]),
	methods: {
		...mapMutations(useLoginUserStore,[
			'login',
			'setOrgName',
			'setSubUrl',
			'clearToken',
			'setToken'
		]),
		bindConfirm(){
			let _that = this
			if(!_that.unitName){
				_that.om.toast("请输入单位全称")
				return
			}
			// if(!_that.userName){
			// 	_that.om.toast("请输入账号姓名")
			// 	return
			// }
			if(!_that.phone){
				_that.om.toast("请输入私人手机号码")
				return
			}
			if(!_that.password){
				_that.om.toast("请输入登录密码")
				return
			}
			
			let formData = {phone:_that.phone};
			let rules = [
				{name:"phone",rule:["required","isMobile"],msg:["请输入手机号","请输入正确的手机号码"]}
			];
			let msg = form.validation(formData,rules);
			if(msg) {
				errorModal(null,"错误提示",msg);
				return 
			}

			uni.request({
				url: _that.omMateServer + 'integrate/getSite',
				method: 'POST',
				data:{
					orgName: _that.unitName,
					phone: _that.phone,
					password: md5.hex_md5(_that.password.toLowerCase())
				},
				success: (res) => {
					if(res.data.success){
						uni.request({
							url: _that.omMateServer + 'integrate/getLoignSite',
							method: 'POST',
							data:{
								phone: _that.phone,
								password: md5.hex_md5(_that.password.toLowerCase())
							},
							success: (res) => {
								res = res.data;
								if(res.success){
									let orgSites = res.data;
									if(orgSites && orgSites.length){
										_that.stateurl = orgSites[0].orgSite
										uni.setStorageSync('ORG_SITE', {
											site: _that.stateurl,
											name: orgSites[0].orgName
										});
										app.globalData.url = _that.stateurl
										_that.toLogin()
									} else {
										_that.om.toast('用户名或密码错误！');
									}
								}
							}
						})
					}else{
						uni.showModal({
							title: '提示',
							content: res.data.message,
							showCancel: false,
							confirmText: '知道了'
						})
					}
				}
			})
			
		},
		toLogin(againFlag){
			let _that = this
			const data = {
				mobile: _that.phone,
				password: md5.hex_md5(_that.password.toLowerCase())
			};
			uni.showLoading({
				title: '登录中'
			});
			_that.om.request('loginCenter/enter', data, 'post', false).then(res => {
				if (res.succeed) {
					_that.setToken(res.value.token)
					_that.toMain(res.value.accountInfo);
					let obj = {
						mobile: res.value.accountInfo.mobile,
						name: res.value.accountInfo.username,
						imgSrc: res.value.accountInfo.avatar
					}
					uni.setStorageSync('ORG_LOGIN', JSON.stringify(obj));
				} else {
					if(againFlag){
						uni.removeStorageSync('ORG_SITE')
						_that.bindLogin();
					} else {
						uni.hideLoading()
						_that.om.toast(res.msg);
						_that.accountFocus = true
					}
				}
			});
		},
		toMain(sessionInfo) {
			if (!sessionInfo || !sessionInfo.userid) {
				this.om.clearToken();
				this.om.toast("登录失败，请检查用户名或密码是否正确！")
			} else {
				this.login(sessionInfo);
				this.om.setToken(this.phone, this.password);
				if (this.hasLogin) {
					if (sessionInfo.orgid) {
						uni.switchTab({
							url: '/pages/main/main/main'
						});
					} else {
						uni.navigateTo({
							url: "/pages/login/orgins/orgins"
						});
					}
				}
			}
		},
	},
	onLoad(e) {
		let _that = this;
		uni.getStorage({
			key: "ORG_SITE",
			success(res) {
				if (res.data) {
					_that.stateurl = res.data.site
				}
			}
		})
	},
};
</script>

<style scoped>
	.input-row .title{
		width: 30%;
		text-align: right;
		font-size: 16px;
		padding-left: 8px;
	}
	.wxts{
		padding: 18px;
		font-size: 14px;
	}
	.wxts .single{
		padding-left: 28px;
		margin-bottom: 5px;
	}
	.m-input-view{
		padding: 0;
	}
</style>
